﻿Imports SdlDotNet.Graphics
Imports SdlDotNet.Core
Imports SdlDotNet.Input

Module AtomicFury

    Private CurrentGame As Game
    Private Display As Surface

    Sub Main()
        InitializeSDL()
        InitializeGame()
        Run()
    End Sub

    Private Sub InitializeSDL()
        Video.WindowCaption = "Atomic Fury v0.9.0"
        Display = Video.SetVideoMode(800, 600, False, False, False, False, True)
        Mouse.ShowCursor = False

        AddHandler Events.Quit, AddressOf HandleQuit
        AddHandler Events.KeyboardUp, AddressOf HandleKeyboardUp
        AddHandler Events.MouseMotion, AddressOf HandleMouseMotion
        AddHandler Events.MouseButtonUp, AddressOf HandleMouseButtonUp
    End Sub

    Private Sub HandleQuit(ByVal sender As Object, ByVal e As QuitEventArgs)
        CurrentGame.State = GameState.Quit
    End Sub

    Private Sub HandleKeyboardUp(ByVal sender As Object, ByVal e As KeyboardEventArgs)
        CurrentGame.ScreenToDisplay.HandleKeyboardUp(sender, e)
    End Sub

    Private Sub HandleMouseButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        CurrentGame.ScreenToDisplay.HandleMouseButtonUp(sender, e)
    End Sub

    Private Sub HandleMouseMotion(ByVal sender As Object, ByVal e As MouseMotionEventArgs)
        CurrentGame.ScreenToDisplay.HandleMouseMotion(sender, e)
    End Sub

    Private Sub InitializeGame()
        Cursor.Instance()
        CurrentGame = New Game()
    End Sub

    Private Sub Run()
        While (CurrentGame.State <> GameState.Quit)
            PollEvents()
            Cursor.Instance().Position = Mouse.MousePosition
            UpdateScreen()
        End While
    End Sub

    Private Sub PollEvents()
        While (Events.Poll())
        End While
    End Sub

    Private Sub UpdateScreen()
        CurrentGame.ScreenToDisplay.Draw(Display)
        Cursor.Instance().Draw(Display)
        Display.Unlock()
        Display.Update()
    End Sub

End Module
